package com.zy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.domain.Member;
import com.zy.domain.User;
import com.zy.mapper.MemberMapper;
import com.zy.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
  @Autowired
  private MemberMapper memberMapper;
  @Autowired
  private UserServiceImpl userService;
  @Override
  public Member becomeMember(String id, Integer memberLevel, Integer memberType) {
//    memberLevel 会员等级 1 初级 2 中级 3 高级会员
//      memberType 会员类型 0 月费 , 1 年费
    User user = userService.selectUserById(id);
    if (user==null){
      throw new RuntimeException("此用户不存在!");
    }
    Double taskMoney = user.getTaskMoney();
    switch (memberLevel){
      case 1:
        if (memberType==0){
          taskMoney=taskMoney-30;
        }
        if (memberType==1){
          taskMoney=taskMoney-300;
        }
        break;
      case 2:
        if (memberType==0){
          taskMoney=taskMoney-80;
        }
        if (memberType==1){
          taskMoney=taskMoney-700;
        }
        break;
      case 3:
        if (memberType==0){
          taskMoney=taskMoney-200;
        }
        if (memberType==1){
          taskMoney=taskMoney-1600;
        }
        break;
    }
    if (taskMoney<0){
      return  null;
    }
    user.setTaskMoney(taskMoney);
    userService.updateById(user);
    Member member=new Member();
    member.setUid(id.toString());
    member.setLv(memberLevel);
    //查看充值的用户是否是会员
//    User user1 = userService.selectUserById(id);
//    memberMapper.selectMemberByUid(id);
//    user.get
    Member member1 = this.selectMemberByUid(id);
    if (member1==null){//用户不是会员的情况
      //当前时间
      Date date = new Date();
      member.setStartTime(date);
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      if (memberType==0){
//        月费会员，到期时间加一个月
        cal.add(Calendar.MONTH, 1);//增加一个月
        member.setEndTime(cal.getTime());
//        cal
        cal.add(Calendar.MONTH, -1);//复原
      }
      if (memberType==1){
//        年费会员，到期时间加一个年
        cal.add(Calendar.YEAR, 1);//增加一年
        member.setEndTime(cal.getTime());
        cal.add(Calendar.YEAR, -1);//复原
      }
      int insert = memberMapper.insert(member);
      return  insert>0?member:null;
    }
    else {//用户是会员的情况
      Date endTime = member1.getEndTime();
      Calendar instance = Calendar.getInstance();
      instance.setTime(endTime);
      member1.setLv(memberLevel);
     if (memberType==0){
       //充值月费会员
       instance.add(Calendar.MONTH,1);//日期加一个月
       member1.setEndTime(instance.getTime());
       instance.add(Calendar.MONTH,-1);//复原
     }
      if (memberType==0){
      //充值年费会员
        instance.add(Calendar.YEAR,1);//日期加一年
        member1.setEndTime(instance.getTime());
        instance.add(Calendar.YEAR,-1);//复原
      }
      return member1;
    }

  }

  @Override
  public Member selectMemberByUid(String uid) {
    LambdaQueryWrapper<Member> eq = new LambdaQueryWrapper<Member>().eq(Member::getUid, uid);
    Member member = memberMapper.selectOne(eq);
    return member;
  }
}
